package adapter;

import java.util.ArrayList;

import model.Video;
import model.film;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.adgroup.doremon_ad.R;
import com.androidquery.AQuery;

public class AdapterSearchVideo extends BaseAdapter {

	public ArrayList<Video> films;
	detailfilm detail;
	Context context;
	IOnClickItemSearchVideo mOnClickItemVideo;
	private static ImageButton mMoreIv;

	public interface IOnClickItemSearchVideo{
		void onClickItemVideo(int position, View v);
	}
	public AdapterSearchVideo(Context context, ArrayList<Video> films) {
		super();
		this.films = films;
		this.context = context;
		mOnClickItemVideo = (IOnClickItemSearchVideo) context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return films.size();
	}

	@Override
	public Object getItem(int pos) {
		// TODO Auto-generated method stub
		return films.get(pos);
	}

	@Override
	public long getItemId(int pos) {
		// TODO Auto-generated method stub
		return pos;
	}

	@Override
	public View getView(final int position, View view, ViewGroup arg2) {
		// TODO Auto-generated method stub
		LayoutInflater inflater = ((Activity) context).getLayoutInflater();
		if (view == null) {			
			detail = new detailfilm();
			view = inflater.inflate(R.layout.item_video, null);
			detail.images = (ImageView) view.findViewById(R.id.imgvideo);
			detail.title = (TextView) view.findViewById(R.id.namewvideo);
			detail.timedur = (TextView) view.findViewById(R.id.durvideo);
			detail.btn = (ImageButton) view.findViewById(R.id.imageButtonDetail);
			view.setTag(detail);
		} else {
			detail = (detailfilm) view.getTag(); 
		}
		AQuery aq = new AQuery(view);
		Bitmap placeholder = aq.getCachedImage(R.drawable.imageload);
		aq.id(detail.images).image(films.get(position).getLink(), true, true,
				0, 0, placeholder, AQuery.FADE_IN_NETWORK, 0.7f);
		String timeDuration = (films.get(position).getDuration());
		detail.timedur.setText("Time: " + timeDuration+" - "+films.get(position).getViews()+" "+context.getResources().getString(R.string.views)+", "+films.get(position).getLikes()+" "+context.getResources().getString(R.string.likes));
		String title = films.get(position).getTitle();
		if (title.length() > 60) {
			String temp = title.substring(0, 60);
			title = temp.substring(0, temp.lastIndexOf(' ')) + "...";
		} else {
			title = films.get(position).getTitle();
		}  
		detail.title.setText(title);
		detail.btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				mOnClickItemVideo.onClickItemVideo(position,arg0);
				mMoreIv = (ImageButton) arg0.findViewById(R.id.imageButtonDetail);
				mMoreIv.setImageResource(R.drawable.details_press);
			}
		}); 
		return view;
	}

	public String SecondsToTimer(long seconds) {
		String finalTimerString = "";
		String secondsString = "";

		// chuyen doi thoi gian cua bai hat
		int gio = (int) (seconds / (3600));
		int phut = (int) (seconds % 3600) / (60); // so phut con du
		int giay = (int) ((seconds % 3600) % 60);// so giay
													// con
													// du
		// kiem tra gio
		if (gio > 0) {
			finalTimerString = gio + ":";
		}

		// dinh dang lai giay
		if (giay < 10) {
			secondsString = "0" + giay;
		} else {
			secondsString = "" + giay;
		}

		finalTimerString = finalTimerString + phut + ":" + secondsString;

		// tra ve thoi gian cua bai hat dang chuoi
		return finalTimerString;
	}

	public void updateVideo(ArrayList<Video> videos) {
		for (Video video : videos) {
			this.films.add(video);
		}
		this.notifyDataSetChanged();
	}

	public void resetAdapter() {
		this.films.clear();
		notifyDataSetChanged();
	}

	public void resetButtonDetail() {
		mMoreIv.setImageResource(R.drawable.details);
	}

	public void removeVideo(String id) {
		for (Video v : this.films) {
			if((""+v.getId()).equalsIgnoreCase(id)){
				this.films.remove(v);
				break;
			}
		}
		notifyDataSetChanged();
	}
}
